package cn.itsource.easyexcel.write;

import cn.itsource.constant.Constant;
import cn.itsource.vo.ImageData;
import com.alibaba.excel.EasyExcel;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 写入图片到excel中
 */
public class WriteDemo10 {
    public static void main(String[] args) throws Exception{
        String filename =  Constant.BATH_URL + "user10.xlsx";
        // 图片位置
        String imagePath =  Constant.BATH_URL + "20210225095455.jpg";
        // 网络图片
        URL url = new URL("https://itsource-typora-oss.oss-cn-chengdu.aliyuncs.com/typora-img/20210225095455.jpg");
        // 将图片读取到二进制数据中
        byte[] bytes = new byte[(int) new File(imagePath).length()];
        InputStream inputStream = new FileInputStream(imagePath);
        inputStream.read(bytes, 0, bytes.length);

        List<ImageData> imageDataList = new ArrayList<>();

        // 创建数据模板
        ImageData imageData = ImageData.builder()
                .file(new File(imagePath))
                .inputStream(new FileInputStream(imagePath))
                .str(imagePath)
                .byteArr(bytes)
                .url(url)
                .build();
        // 添加要写入的图片模型
        imageDataList.add(imageData);

        // 写数据
        EasyExcel.write(filename, ImageData.class)
                .sheet("源码")
                .doWrite(imageDataList);
    }
}
